package com.andova.module.app;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;

import com.andova.manager.AndovaModuleManager;
import com.andova.module.api.IModuleDelegate;
import com.andova.module.view.CheckedGridView;
import com.andova.module.view.UncheckedGridView;

/**
 * Created by Administrator on 2018-03-20.
 *
 * @author kzaxil
 * @since 1.0.0
 */
public class ModuleDelegate implements IModuleDelegate {
    private Activity mActivity;

    private CheckedGridView mCheckedGridView;
    private UncheckedGridView mUncheckedGridView;

    public ModuleDelegate(@NonNull Activity activity) {
        mActivity = activity;
    }

    @Override
    public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
        if (!name.equals("GridView")) return mActivity.onCreateView(parent, name, context, attrs);
        for (int i = 0; i < attrs.getAttributeCount(); i++) {
            if (!attrs.getAttributeName(i).equals("id")) continue;
            int value = attrs.getAttributeResourceValue(i, 0);
            if (value == AndovaModuleManager.obj().module().checkedGridViewId(mActivity)) {
                mCheckedGridView = new CheckedGridView(context, attrs);
                return mCheckedGridView;
            } else if (value == AndovaModuleManager.obj().module().uncheckedGridViewId(mActivity)) {
                mUncheckedGridView = new UncheckedGridView(context, attrs);
                return mUncheckedGridView;
            }
        }
        return mActivity.onCreateView(parent, name, context, attrs);
    }

    @Override
    public CheckedGridView checkedGridView() {
        return mCheckedGridView;
    }

    @Override
    public UncheckedGridView uncheckedGridView() {
        return mUncheckedGridView;
    }
}
